package com.kefu.admin.modules.sys.service.impl;


import com.kefu.admin.modules.sys.service.SysDeptService;
import com.kefu.admin.modules.sys.mapper.SysDeptMapper;
import com.kefu.admin.modules.sys.model.SysDeptEntity;
import com.kefu.admin.modules.sys.model.SysDeptTreeModel;
import com.kefu.admin.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service("sysDeptService")
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;


	@Override
	public String getSubDeptIdList(String deptId) {
		//部门及子部门ID列表
		List<String> deptIdList = new ArrayList<>();

		//获取子部门ID
		List<String> subIdList = queryDetpIdList(deptId);
		getDeptTreeList(subIdList, deptIdList);

		//添加本部门
		deptIdList.add(deptId);

		String deptFilter = StringUtils.join(deptIdList, ",");
		return deptFilter;
	}

	@Override
	public List<String> queryDetpIdList(String parentId) {
		return sysDeptMapper.queryDetpIdList(parentId);
	}

	@Override
	public List<SysDeptTreeModel> queryList(Map<String, Object> map) {
		return sysDeptMapper.queryList(map);
	}

	@Override
	public void save(SysDeptEntity dept) {
		sysDeptMapper.save(dept);
	}

	@Override
	public void update(SysDeptEntity dept) {
		sysDeptMapper.update(dept);
	}

	@Override
	public void delete(String deptId) {
		sysDeptMapper.deleteDept(deptId);
	}

	/**
	 * 递归
	 */
	public void getDeptTreeList(List<String> subIdList, List<String> deptIdList){
		for(String deptId : subIdList){
			List<String> list = queryDetpIdList(deptId);
			if(list.size() > 0){
				getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}


}
